using Microsoft.Extensions.Options;
using Redis_demo.Extensions;
using Redis_demo.Interceptors;
using Redis_demo.Middlewares;
using Redis_demo.Options;
using Redis_demo.Services;
using StackExchange.Redis;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers(options =>{
    //options.Filters.Add<Userinterceptor>();
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

//builder.Services.AddAuthentication().AddCookie("ss");
builder.Services.AddHttpContextAccessor();

// 添加其他注入
builder.Services.AddRedisOptions(builder.Configuration);
builder.Services.RegisterRedisConnect();
builder.Services.AddUserDbContext(builder.Configuration);
builder.Services.AddScoped<UserServices>();
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

builder.Services.AddSingleton<RefreshTokenMiddleware>();
#region  ai
// 注册RedisSettings配置 net 3.1
//builder.Services.AddOptions().Configure<RedisSettings>(builder.Configuration.GetSection("RedisSettings"));
// net 6
//builder.Configuration.Bind("Redis",new RedisSettings());
// 注册单例的ConnectionMultiplexer连接对象
// ConfigurationBuilder configBuilder = new ConfigurationBuilder();
// configBuilder.AddJsonFile("appsettings.json");
// var host = configBuilder.Build().GetSection("RedisSettings:Host").Value;

// var tee = builder.Configuration.GetSection("RedisSettings:Host").Value;

// builder.Configuration.AddJsonFile("appsettings.json",false,true);
// builder.Services.AddOptions().Configure<RedisSettings>(e => builder.Configuration.GetSection("RedisSettings").Bind(e));

// builder.Services.AddSingleton<IConnectionMultiplexer>(provider =>
// {
//     var redisSettings = provider.GetRequiredService<IOptions<RedisSettings>>().Value;
//     var options = new ConfigurationOptions
//     {
//         EndPoints = { $"{redisSettings.Host}:{redisSettings.Port}" },
//         Password = redisSettings.Password
//     };
//     return ConnectionMultiplexer.Connect(options);
// });
#endregion


var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();

app.UseMiddleware<RefreshTokenMiddleware>();

app.MapControllers();

app.Run();
